package com.aierjun.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aierjun.myapplication.utils.BitmapUtil;
import com.aierjun.myapplication.utils.GetPathFromUri4kitkat;
import com.aierjun.myapplication.view.DateDialog;
import com.aierjun.myapplication.view.ImageChangeDialog;
import com.aierjun.myapplication.view.MyDelWeightDialog;
import com.aierjun.myapplication.view.PickerView;
import com.aierjun.myapplication.view.PickerViewDialog;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ani_aierJun on 2017/9/6.
 */

public class EvaluateActivity extends Activity implements View.OnClickListener {
    private ImageView back;
    private TextView commit;
    private EditText name;
    private EditText car_number;
    private TextView car_type;
    private TextView date;
    private EditText hope_money;
    private EditText loan_money;
    private EditText compony_ratio;
    private ImageView car_photo;
    private ImageView drive_photo;
    private ImageView products_photo;
    private LinearLayout car_photo_l;
    private LinearLayout drive_photo_l;
    private LinearLayout products_photo_l;
    private LinearLayout evaluate_main_l;

    private LinearLayout linearLayoutNew;
    private LinearLayout linearLayoutNew1;
    private LinearLayout linearLayoutNew2;

    private RelativeLayout car_photo_r;
    private ImageView car_photo_del;

    private ImageChangeDialog imageChangeDialog;
    private ImageView photoAndCream;
    private LinearLayout linearLayoutNow;
    private MyDelWeightDialog myDelWeightDialog;
    private String dateString;
    private DateDialog dateDialog;

    private List<String> list_1,list_2 ,list_3;
    private PickerViewDialog pickerViewDialog;
    private String pickerView_Text_1,pickerView_Text_2;
    private List<List<String>> lists ;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate);
        findView();
        initView();
    }

    private void findView() {
        back = (ImageView) findViewById(R.id.back);
        commit = (TextView) findViewById(R.id.commit);
        name = (EditText) findViewById(R.id.name_ed);
        car_number = (EditText) findViewById(R.id.car_number_ed);
        car_type = (TextView) findViewById(R.id.car_type_text);
        date = (TextView) findViewById(R.id.date_text);
        hope_money = (EditText) findViewById(R.id.hope_money_ed);
        loan_money = (EditText) findViewById(R.id.loan_money_ed);
        compony_ratio = (EditText) findViewById(R.id.compony_money_ratio_ed);
        car_photo = (ImageView) findViewById(R.id.car_photo_add);
        drive_photo = (ImageView) findViewById(R.id.drive_photo_add);
        products_photo = (ImageView) findViewById(R.id.products_photo_add);
        car_photo_l = (LinearLayout) findViewById(R.id.car_photo_l);
        drive_photo_l = (LinearLayout) findViewById(R.id.drive_photo_l);
        products_photo_l = (LinearLayout) findViewById(R.id.products_photo_l);
        evaluate_main_l = (LinearLayout) findViewById(R.id.evaluate_main_l);
        car_photo_r = (RelativeLayout) findViewById(R.id.car_photo_r);
        car_photo_del = (ImageView) findViewById(R.id.car_photo_del);

    }

    private void initView() {
        back.setOnClickListener(this);
        commit.setOnClickListener(this);
        car_type.setOnClickListener(this);
        car_photo.setOnClickListener(this);
        drive_photo.setOnClickListener(this);
        products_photo.setOnClickListener(this);


        date.setOnClickListener(this);

        initData();
    }

    private void initData(){
        //test data
        list_1 = new ArrayList<>();
        list_2 = new ArrayList<>();
        list_1.add("国产车");
        list_1.add("进口车");
        list_2.add("1");
        list_2.add("2");
        list_2.add("3");
        list_2.add("4");
        list_3 = new ArrayList<>();
        list_3.add("5");
        list_3.add("6");
        list_3.add("7");
        list_3.add("8");
        lists = new ArrayList<>();
        lists.add(list_2);
        lists.add(list_3);

        pickerViewDialog = new PickerViewDialog(this,2);
        pickerViewDialog.showGravity(Gravity.BOTTOM);
//        pickerViewDialog.setData(list_1 , new PickerView.onSelectListener() {
//            @Override
//            public void onSelect(String text) {
//                pickerView_Text_1 = text;
//                Log.d("Jun" , text);
//                pickerViewDialog.setData2(list_3,new PickerView.onSelectListener() {
//                    @Override
//                    public void onSelect(String text) {
//                        pickerView_Text_2 = text;
//                        Log.d("Jun" , text);
//                    }
//                });
//            }
//        });
        pickerViewDialog.setDataList(list_1, new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                pickerView_Text_1 = text;
                Log.d("Jun" ,"  111111111111  " + text);
            }
        }, lists, new PickerView.onSelectListener() {
            @Override
            public void onSelect(String text) {
                pickerView_Text_2 = text;
                Log.d("Jun" ,"  222222222222  " + text);
            }
        });
        pickerViewDialog.setOnClickLister(new PickerViewDialog.OnClickLister() {
            @Override
            public void CLickLeft(View view) {
                pickerViewDialog.dismiss();
            }

            @Override
            public void ClickRight(View view) {
                if (pickerView_Text_1 == null || pickerView_Text_1.equals(""))
                    pickerView_Text_1 = "";
                if (pickerView_Text_2 == null || pickerView_Text_2.equals(""))
                    pickerView_Text_2 = "";
                car_type.setText(pickerView_Text_1 + "  " + pickerView_Text_2);
                pickerViewDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                if (myDelWeightDialog == null) {
                    myDelWeightDialog = new MyDelWeightDialog(this) {
                        @Override
                        protected void checkLeft(TextView textView) {
                            //取消
                            Log.d("Jun ", "取消");
                            finish();
                        }

                        @Override
                        protected void checkRight(TextView textView) {
                            //保存
                            Log.d("Jun ", "保存");
                        }
                    };
                    myDelWeightDialog.setDelMes("当前未保存，确定继续退出？");
                    myDelWeightDialog.setLeftMes("取消");
                    myDelWeightDialog.setRightMes("保存");
                    myDelWeightDialog.show();
                }
                break;
            case R.id.commit:
                //提交
                break;
            case R.id.car_type_text:
                pickerViewDialog.show();
                break;
            case R.id.car_photo_add:
                photoCreamChoose(car_photo_l);
                break;
            case R.id.drive_photo_add:
                photoCreamChoose(drive_photo_l);
                break;
            case R.id.products_photo_add:
                photoCreamChoose(products_photo_l);
                break;
            case R.id.date_text:
                if (dateDialog == null)
                    dateDialog = new DateDialog(this);
                dateDialog.setCallBackDate(new DateDialog.CallBackDate() {
                    @Override
                    public void callBackDateString(String d) {
                        Log.d("Jun date ", d);
                        dateString = d;
                    }

                    @Override
                    public void clickLeft(View view) {
                        Log.d("Jun dateDialog ", "clickLeft");
                        dateDialog.dismiss();
                    }

                    @Override
                    public void clickRight(View view) {
                        Log.d("Jun dateDialog ", "clickRight");
                        if (dateString == null || dateString.equals("")) {
                            SimpleDateFormat format = new SimpleDateFormat(
                                    "yyyy/MM/dd");
                            date.setText(format.format(System.currentTimeMillis()));
                        } else{
                            date.setText(dateString);
                        }
                        dateDialog.dismiss();
                    }
                });
                dateDialog.showGravity(Gravity.CENTER).show();
                break;
            default:
                RelativeLayout relativeLayout = (RelativeLayout) v.getParent();
                LinearLayout linearLayout = (LinearLayout) relativeLayout.getParent();
                linearLayout.removeView(relativeLayout);
                if (linearLayout.getId() == R.id.car_photo_l) {
                    if (linearLayoutNew != null && linearLayoutNew.getChildCount() > 0) {
                        RelativeLayout relativeLayoutNew = (RelativeLayout) linearLayoutNew.getChildAt(0);
                        linearLayoutNew.removeView(relativeLayoutNew);
                        linearLayout.addView(relativeLayoutNew);
                        if (linearLayoutNew1 != null && linearLayoutNew1.getChildCount() > 0) {
                            RelativeLayout relativeLayout1 = (RelativeLayout) linearLayoutNew1.getChildAt(0);
                            linearLayoutNew1.removeView(relativeLayout1);
                            linearLayoutNew.addView(relativeLayout1);
                            if (linearLayoutNew2 != null && linearLayoutNew2.getChildCount() > 0) {
                                RelativeLayout relativeLayout2 = (RelativeLayout) linearLayoutNew2.getChildAt(0);
                                linearLayoutNew2.removeView(relativeLayout2);
                                linearLayoutNew1.addView(relativeLayout2);
                            }
                        }
                    }
                }
                if (linearLayout == linearLayoutNew) {
                    if (linearLayoutNew1 != null && linearLayoutNew1.getChildCount() > 0) {
                        RelativeLayout relativeLayout1 = (RelativeLayout) linearLayoutNew1.getChildAt(0);
                        linearLayoutNew1.removeView(relativeLayout1);
                        linearLayoutNew.addView(relativeLayout1);
                        if (linearLayoutNew2 != null && linearLayoutNew2.getChildCount() > 0) {
                            RelativeLayout relativeLayout2 = (RelativeLayout) linearLayoutNew2.getChildAt(0);
                            linearLayoutNew2.removeView(relativeLayout2);
                            linearLayoutNew1.addView(relativeLayout2);
                        }
                    }
                }
                if (linearLayout == linearLayoutNew1) {
                    if (linearLayoutNew2 != null && linearLayoutNew2.getChildCount() > 0) {
                        RelativeLayout relativeLayout2 = (RelativeLayout) linearLayoutNew2.getChildAt(0);
                        linearLayoutNew2.removeView(relativeLayout2);
                        linearLayoutNew1.addView(relativeLayout2);
                    }
                }
                break;
        }
    }

    public View createImage(LinearLayout linearLayout) {
        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setLayoutParams(car_photo_r.getLayoutParams());
        ImageView imageView = new ImageView(this);
        RelativeLayout.LayoutParams relativeLayoutImg = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 150);
        imageView.setLayoutParams(relativeLayoutImg);
        imageView.setPadding(0, 10, 0, 0);
        imageView.setImageDrawable(getResources().getDrawable(R.mipmap.expressbg));
        ImageView imageViewdel = new ImageView(this);
        imageViewdel.setLayoutParams(car_photo_del.getLayoutParams());
        imageViewdel.setImageDrawable(getResources().getDrawable(R.drawable.untitled));
        relativeLayout.addView(imageView, 0);
        relativeLayout.addView(imageViewdel, 1);
        imageViewdel.setEnabled(true);
        imageViewdel.setOnClickListener(this);
        if (linearLayout.getChildCount() < 4) {
            if (linearLayout.getId() != R.id.products_photo_l)
                linearLayout.addView(relativeLayout);
            else {
                if (linearLayout.getChildCount() < 2)
                    linearLayout.addView(relativeLayout);
            }
        } else {
            if (linearLayout.getId() != R.id.car_photo_l)
                return imageView;
            if (linearLayoutNew == null) {
                linearLayoutNew = new LinearLayout(this);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.bottomMargin = 10;
                layoutParams.gravity = Gravity.LEFT;
                linearLayoutNew.setLayoutParams(layoutParams);
                linearLayoutNew.setWeightSum(4);
                evaluate_main_l.addView(linearLayoutNew, 19);
            }
            if (linearLayoutNew.getChildCount() < 4)
                linearLayoutNew.addView(relativeLayout);
            else {
                if (linearLayoutNew1 == null) {
                    linearLayoutNew1 = new LinearLayout(this);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    layoutParams.bottomMargin = 10;
                    layoutParams.gravity = Gravity.LEFT;
                    linearLayoutNew1.setLayoutParams(layoutParams);
                    linearLayoutNew1.setWeightSum(4);
                    evaluate_main_l.addView(linearLayoutNew1, 20);
                }
                if (linearLayoutNew1.getChildCount() < 4)
                    linearLayoutNew1.addView(relativeLayout);
                else {
                    if (linearLayoutNew2 == null) {
                        linearLayoutNew2 = new LinearLayout(this);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        layoutParams.bottomMargin = 10;
                        layoutParams.gravity = Gravity.LEFT;
                        linearLayoutNew2.setLayoutParams(layoutParams);
                        linearLayoutNew2.setWeightSum(4);
                        evaluate_main_l.addView(linearLayoutNew2, 21);
                    }
                    if (linearLayoutNew2.getChildCount() < 4)
                        linearLayoutNew2.addView(relativeLayout);
                }
            }
        }
        Log.d("Jun num ", linearLayout.getChildCount() + "");
        return imageView;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                try {
                    if (data == null || data.getExtras() == null) return;
                    photoAndCream = (ImageView) createImage(linearLayoutNow);
                    Bundle bundle = data.getExtras();
                    Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据，并转换为Bitmap图片格式
                    photoAndCream.setImageBitmap(BitmapUtil.rotateBitmapSize(bitmap));
                } catch (Exception e) {
                    System.out.println("请拍照");
                }
                break;
            case 2:
                try {
                    if (data == null || data.getData() == null) return;
                    photoAndCream = (ImageView) createImage(linearLayoutNow);
                    Uri uri = data.getData();
                    Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
                    cursor.moveToFirst();
                    String imgNo = cursor.getString(0); // 图片编号
                    String imgPath = cursor.getString(1); // 图片文件路径
                    String imgSize = cursor.getString(2); // 图片大小
                    String imgName = cursor.getString(3); // 图片文件名
                    String path = GetPathFromUri4kitkat.getPath(this, uri);
                    if (path.equals("") || path == null)
                        path = imgPath;
                    Bitmap b = BitmapFactory.decodeFile(path);
                    photoAndCream.setImageBitmap(BitmapUtil.rotateBitmapSize(b));
                } catch (Exception e) {
                    System.out.println("请选择图片");
                }
                break;
        }
    }

    private void photoCreamChoose(final LinearLayout linearLayout) {
        if (imageChangeDialog == null)
            imageChangeDialog = new ImageChangeDialog(EvaluateActivity.this);
        imageChangeDialog.showGravity(Gravity.BOTTOM).show();
        imageChangeDialog.setOnClickLister(new ImageChangeDialog.OnClickLister() {
            @Override
            public void CLickCream(View view) {
                Log.d("Jun ", "CLickCream");
                linearLayoutNow = linearLayout;
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 1);
            }

            @Override
            public void ClickPhoto(View view) {
                Log.d("Jun ", "ClickPhoto");
                linearLayoutNow = linearLayout;
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
                startActivityForResult(intent, 2);
            }

            @Override
            public void ClickCancal(View view) {
                Log.d("Jun ", "ClickCancal");
            }
        });
    }

    @Override
    protected void onDestroy() {
        pickerViewDialog = null;
        super.onDestroy();
    }
}
